package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.form.impl.FootUserSearchForm;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
@SessionAttributes("footUserSearchForm")
@RequestMapping("/foot/footUser")
public class FootUserController extends AddAndEditController<FootUser>{
	/* 实体名称 */
	private static final String ENTITYNAME = Constants.USER;
	private static final String EDITPAGE = String.format(Constants.EDITPAGE, Constants.FOOT, ENTITYNAME);
	private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
	private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);
	private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.FOOT, ENTITYNAME);

	@Autowired
	private FootUserService footUserService;

    /**
     * 抽象方法
     *
     * @return 实体名称
     */
    @Override
    protected String getEntityName() {
        return ENTITYNAME;
    }

    /**
	 * 初始化
	 * @param map 参数
	 * @param footUserSearchForm 条件参数
	 */
	@ModelAttribute
	public void init(Model map, @ModelAttribute FootUserSearchForm footUserSearchForm){
		FootUserSearchForm newFootUserSearchForm = new FootUserSearchForm();
		if(footUserSearchForm != null){
			newFootUserSearchForm = footUserSearchForm;
		}
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootUserSearchForm);
	}

	@RequestMapping(value="/search")
	public String search(Model map, @ModelAttribute FootUserSearchForm footUserSearchForm) {
        footUserService.search(ENTITYNAME, map, footUserSearchForm);
		return LISTPAGE;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(@RequestParam Long id, HttpServletRequest request) {
		footUserService .delete(id, request);
		return REDIRECT;
	}

	@RequestMapping(value = "/deleteall", method = RequestMethod.POST)
	public String deleteall(HttpServletRequest request, @ModelAttribute FootUserSearchForm footUserSearchForm) {
        footUserService.deleteAll(footUserSearchForm, request);
		return REDIRECT;
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String openEditPage(Model map, @RequestParam Long id) {
		map.addAttribute(ENTITYNAME, footUserService.get(id));
		return EDITPAGE;
	}
	
	@RequestMapping(value = "/editVip", method = RequestMethod.GET)
	public String openEditVipPage(Model map, @RequestParam Long id) {
		map.addAttribute(ENTITYNAME, footUserService.get(id));
		return "foot/footUser/EditVip";
	}
	
	@RequestMapping(value = "/editVip", method = RequestMethod.POST)
	public String updateVip(@Valid @ModelAttribute FootUser footUser, BindingResult bindingResult,
			HttpServletRequest request, SessionStatus s){
		if (!bindingResult.hasErrors()) {
			footUserService .changeUser2Vip(footUser, request);
			s.setComplete();
			return REDIRECT;
		} else {
			return "foot/footUser/EditVip";
		}
	}

	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootUserSearchForm footUserSearchForm) {
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footUserSearchForm);
		map.addAttribute(ENTITYNAME, footUserService.get(id));
		return VIEWPAGE;
	}	
		
}
